﻿<template>
    <div id="app" style="">
        <Spin size="large" fix v-if="spinShow"></Spin>
     
        <router-view></router-view>

        <tabbar :selected="selected" :propindex="index"></tabbar>
    </div>
</template>

<script>

    import tabbar from './components/tabbar.vue';
    import { Toast } from 'mint-ui';
    import Bus from './bus.js'

 
    export default {
        name: 'app',
        data() {
            return {
                spinShow: true, 
                selected: '首页',
                index: 1,
            }
        },
        components: {
          
            'tabbar': tabbar,

        },
        beforeRouteLeave(to, from, next) {
          
        },
        mounted() {
            window.history.pushState('forward', null, '#');
            window.history.forward(1);

            window.onpopstate = () => {


            }
        },
        methods: {
            load: function () {
                var _this = this;
                var code = this.GetRequest("code");
                this.$api.post('wx/Index/getaccess_token', {
                    "code": code
                }, response => {

                    console.log(response.data);
                 
                    if (response.status==200) {
                        console.log("结果" + response.data);
                 
                        var data = response.data;
                        _this.$data.spinShow = false;
                        window.localStorage.setItem('openid', data.data.openid);
                        window.localStorage.setItem('nickname', data.data.nickname);
                        window.localStorage.setItem('headimgurl', data.data.headimgurl);

                        //  data.message = "未绑定";
                      
                        if (data.message == "登录成功") {

                            _this.$router.replace({ name: 'home', params: { propindex: 1 } });
                            Bus.$emit('val', "show")
                            window.localStorage.setItem('mid', data.data.mid);
                         
                    
                            Bus.$emit('usertype', data.data.type)
                            window.localStorage.setItem('usertype', data.data.type);
                            window.localStorage.setItem('userid', data.data.id);
                            window.localStorage.setItem('truename', data.data.truename);
                            window.localStorage.setItem('phone', data.data.phone);
                            window.localStorage.setItem('token', data.data.token);

                            if (data.data.type == "1") {
                                //家长
                                window.localStorage.setItem('teacherid',"");
                                window.localStorage.setItem('studentid', data.data.studentid);
                               
                            } else if (data.data.type == "2") {
                                window.localStorage.setItem('teacherid', data.data.teacherid);
                                window.localStorage.setItem('studentid', "");
                            }


                        } else if (data.message == "未绑定") {
                          
                            window.localStorage.setItem('userid', data.data.id);
                            Bus.$emit('val', "hide")
                            _this.$router.replace({ name: 'login', params: { } });

                        } else if (data.message == "未选择身份") {
                            window.localStorage.setItem('userid', data.data.id);
                            Bus.$emit('val', "hide")
                            _this.$router.replace({ name: 'choose' });

                        } else if (data.status == "500") {
                            Toast(data.message);
                            Bus.$emit('val', "hide");
                            _this.$router.replace({ name: 'login', params: {} });
                        } else {

                            Bus.$emit('val', "show")


                        }


                    } else {
                        
                    }
                   
                        
                   
                   
                });
            },
            GetRequest: function (value) {
                var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
                var object = {};
                if (url.indexOf("?") != -1)//url中存在问号，也就说有参数。  
                {
                    var str = url.substr(1);  //得到?后面的字符串
                    var strs = str.split("&");  //将得到的参数分隔成数组[id="123456",Name="bicycle"];
                    for (var i = 0; i < strs.length; i++) {
                        object[strs[i].split("=")[0]] = strs[i].split("=")[1]
                    }
                }

                return object[value];
            }

        },
        created() {
           

            this.load();
            
        }

    };
</script>

<style>
    body,p,div,header,img {
        padding:0px;
        margin:0px;
    }
</style>
